<?php

namespace common\modules\ad\models;

use common\behaviors\TimestampBehavior;
use common\behaviors\UserRecordBehavior;
use common\components\ActiveRecord;
use Yii;
use yii\helpers\ArrayHelper;

/**
 * This is the model class for table "ad_item".
 *
 * @property string $id
 * @property integer $position_id
 * @property integer $media_type
 * @property string $mediaType
 * @property string $name
 * @property string $link
 * @property string $code
 * @property integer $start_time
 * @property integer $end_time
 * @property string $link_man
 * @property string $link_email
 * @property string $link_phone
 * @property integer $click_count
 * @property integer $status
 * @property integer $created_at
 * @property integer $updated_at
 * @property integer $created_by
 * @property integer $updated_by
 * @property Position $position
 */
class Item extends ActiveRecord
{

    /**
     * @const int 图片广告
     */
    const MEDIA_IMAGE = 0;

    /**
     * @const int 代码广告
     */
    const MEDIA_CODE = 1;

    /**
     * @const int 文字广告
     */
    const MEDIA_TEXT = 2;

    /**
     * @return array
     */
    public static function getMediaArray()
    {
        return [
            static::MEDIA_IMAGE => '图片',
            static::MEDIA_CODE => '代码',
            static::MEDIA_TEXT => '文字',
        ];
    }

    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'ad_item';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['position_id', 'media_type', 'start_time', 'end_time', 'click_count', 'status'], 'integer'],
            [['created_at', 'updated_at', 'created_by', 'updated_by'], 'safe'],
            [['code'], 'string'],
            [['name', 'link_man', 'link_email', 'link_phone'], 'string', 'max' => 60],
            [['link'], 'string', 'max' => 255]
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app/ad', 'ID'),
            'position_id' => Yii::t('app/ad', 'Position ID'),
            'media_type' => Yii::t('app/ad', 'Media Type'),
            'name' => Yii::t('app/ad', 'Name'),
            'link' => Yii::t('app/ad', 'Link'),
            'code' => Yii::t('app/ad', 'Code'),
            'start_time' => Yii::t('app/ad', 'Start Time'),
            'end_time' => Yii::t('app/ad', 'End Time'),
            'link_man' => Yii::t('app/ad', 'Link Man'),
            'link_email' => Yii::t('app/ad', 'Link Email'),
            'link_phone' => Yii::t('app/ad', 'Link Phone'),
            'click_count' => Yii::t('app/ad', 'Click Count'),
            'status' => Yii::t('app/ad', 'Status'),
            'created_at' => Yii::t('app/ad', 'Created At'),
            'updated_at' => Yii::t('app/ad', 'Updated At'),
            'created_by' => Yii::t('app/ad', 'Created By'),
            'updated_by' => Yii::t('app/ad', 'Updated By'),
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {
        $behaviors = parent::behaviors();

        $behaviors[] = [
            'class' => TimestampBehavior::className(),
            'createdAtAttribute' => 'created_at',
            'updatedAtAttribute' => 'updated_at',
            'value' => new \yii\db\Expression('NOW()'),
        ];
    
        $behaviors[] = [
            'class' => UserRecordBehavior::className(),
            'createdByAttribute' => 'created_by',
            'updatedByAttribute' => 'updated_by',
        ];
    
        return $behaviors;
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getPosition()
    {
        return $this->hasOne(Position::className(), [
            'id' => 'position_id',
        ]);
    }

    /**
     * @return string
     */
    public function getMediaType()
    {
        return ArrayHelper::getValue(static::getMediaArray(), $this->media_type);
    }
}
